package com.youlu.campus.web.community.controller;

import com.youlu.campus.entity.UserPO;
import com.youlu.campus.entity.VO.req.CaBuildReq;
import com.youlu.campus.entity.VO.req.CaCollectReq;
import com.youlu.campus.entity.VO.req.H5PayCaptchaRequest;
import com.youlu.campus.entity.pay.statistics.vo.StatisticsReq;
import com.youlu.campus.service.activity.impl.PlatformOtherActivityService;
import com.youlu.campus.service.base.impl.TestReplicaMongoService;
import com.youlu.campus.service.ca.impl.CaCollectService;
import com.youlu.campus.service.ca.impl.CaRecommendService;
import com.youlu.campus.service.statistics.PayButtonStatisticsService;
import com.youlu.campus.service.tencentcloud.TencentCloudCaptchaService;
import com.youlu.campus.web.common.base.RequestContext;
import com.youlu.campus.web.file.FileUploadService;
import com.yuelin.infrastructure.quantum.common.QResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@RestController
public class CaCollectController {

    @Autowired
    private CaCollectService caCollectService;

    @Autowired
    private FileUploadService fileUploadService;

    @Autowired
    private TestReplicaMongoService testReplicaMongoService;

    @Autowired
    private PlatformOtherActivityService platformOtherActivityService;

    @Autowired
    private CaRecommendService caRecommendService;

    @Autowired
    private PayButtonStatisticsService payButtonStatisticsService;

    @Autowired
    private TencentCloudCaptchaService tencentCloudCaptchaService;

    /**
     * 当前用户返回参与活动电子证书模版信息
     * @param caCollectReq
     * @return
     */
    @PostMapping("/mini-app/ca/collect")
    public QResult caCollect(@RequestBody @Validated CaCollectReq caCollectReq){
        caCollectReq.setCurrentUser(RequestContext.getCurUser());
        caCollectReq.setCaType("1");
        return QResult.success(caCollectService.collect(caCollectReq));
    }


    @GetMapping("/public/ca/collect")
    public QResult caCollect(@RequestParam String activityId){
        caCollectService.simulateCollect(activityId);
        return QResult.success(null);
    }

    @GetMapping("/mini-app/ca")
    public QResult caReceive(@RequestParam String activityId,@RequestParam String openId,
                             @RequestParam(value = "rewardType" ,defaultValue = "0") Integer rewardType,
                             @RequestParam(value = "mobile" ,required = false) String  mobile){
        return QResult.success(platformOtherActivityService.findByActivityIdAndOpenId(activityId,openId,rewardType));
    }

    /**
     * 证书推荐购买
     * @return
     */
    @GetMapping("/mini-app/ca/recommend")
    public QResult caRecommend(){
        return QResult.success(caRecommendService.caRecommend(RequestContext.getCurUser()));
    }



    /**
     * 校验用户是否能领取电子证书
     * @param checkUser
     * @return
     */
    @PostMapping("/mini-app/ca/check-user")
    public QResult caCheckUserCollect(@RequestBody @Validated CaCollectReq checkUser){
        checkUser.setCurrentUser(RequestContext.getCurUser());
        return QResult.success(caCollectService.checkUser(checkUser));
    }
    /**
     * 校验用户是否能领取电子证书V2
     * @param checkUser
     * @return
     */
    @PostMapping("/mini-app/v2/ca/check-user")
    public QResult caCheckUserCollectV2(@RequestBody @Validated CaCollectReq checkUser){
        checkUser.setCurrentUser(RequestContext.getCurUser());
        return QResult.success(caCollectService.checkUserV2(checkUser));
    }

    /**
     * 校验用户
     * @param request
     * @return
     */
    @PostMapping("/mini-app/ca/check-captcha")
    public QResult caCheckCaptcha(@RequestBody @Validated H5PayCaptchaRequest request, BindingResult bindingResult, HttpServletRequest httpRequest){
        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        request.setUserIp(httpRequest.getRemoteAddr());
        request.setCurrentUser(RequestContext.getCurUser());
        return QResult.success(tencentCloudCaptchaService.h5PayCaptcha(request));
    }
    /**
     * 校验用户V2
     * @param request
     * @return
     */
    @PostMapping("/mini-app/v2/ca/check-captcha")
    public QResult caCheckCaptchaV2(@RequestBody @Validated H5PayCaptchaRequest request, BindingResult bindingResult, HttpServletRequest httpRequest){
        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        request.setUserIp(httpRequest.getRemoteAddr());
        request.setCurrentUser(RequestContext.getCurUser());
        return QResult.success(tencentCloudCaptchaService.h5PayCaptchaV2(request));
    }
    /**
     * 证书生成
     * @param file
     * @param certificateNo
     * @param activityId
     * @return
     */
    @PostMapping("/ca/build")
    public QResult caBuild(@RequestParam("file") MultipartFile file, @RequestParam("certificateNo")String certificateNo, @RequestParam("activityId")String activityId){
        if(file.isEmpty() || StringUtils.isEmpty(certificateNo)||StringUtils.isEmpty(activityId)){
            return QResult.fail(500,"参数为必传");
        }
        String caUrl = fileUploadService.uploadFile(file);
        CaBuildReq caBuildReq = new CaBuildReq();
        caBuildReq.setActivityId(activityId);
        caBuildReq.setCertificateNo(certificateNo);
        caBuildReq.setCurrentUser(RequestContext.getCurUser());
        caBuildReq.setCaUrl(caUrl);
      //  caCollectService.caBuild(caBuildReq);
        return QResult.success(null);
    }

    @GetMapping("/public/certificate/number")
    public QResult getCertNumber(@RequestParam String activityId){
        return QResult.success(caCollectService.test(activityId,"test"));
    }

    @GetMapping("/public/certificate/build")
    public QResult getCertificateBuild(@RequestParam String activityId,String openid,String caType,String caLevel){
        caCollectService.build(activityId,openid,caType,caLevel);
        return QResult.success(null);
    }
    @GetMapping("/public/mongo/replica")
    public QResult insert(){
        testReplicaMongoService.insert();
        testReplicaMongoService.findAll();
        return QResult.success(null);
    }


    /**
     * 统计支付按钮
     * @param req
     * @return
     */
    @PostMapping("/mini-app/ca/button-statistics")
    public QResult payButtonStatistics(@RequestBody @Validated StatisticsReq req){
        req.setCurrentUser(RequestContext.getCurUser());
        payButtonStatisticsService.doStatistics(req);
        return QResult.success(null);
    }

    /**
     * 获取用户最新纸质证书
     * @param activityId
     * @return
     */
    @GetMapping("/mini-app/paper-ca/receive-tips")
    public QResult caPaperReceive(@RequestParam String activityId){
        return QResult.success(caCollectService.getUserCaPaperReceiveByActivityId(RequestContext.getCurUser(),activityId));
    }
    /**
     * 获取用户最新纸质证书
     * @param activityId
     * @return
     */
    @GetMapping("/mini-app/v2/paper-ca/receive-tips")
    public QResult caPaperReceiveV2(@RequestParam String activityId,
                                    @RequestParam(value = "mobile",required = false) String mobile){
        UserPO userPO=RequestContext.getCurUser();
        if(StringUtils.isNotBlank(mobile)){
            userPO.setMobile(mobile);
        }
        return QResult.success(caCollectService.getUserCaPaperReceiveByActivityIdV2(userPO,activityId));
    }

}
